//
//  TotalViewController.m
//  11111
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "TotalViewController.h"
#import "TotalCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "DetailViewController.h"
#import "TotalSubCollectionViewCell.h"

@interface TotalViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@end

@implementation TotalViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addFlowLayout];
}


- (void)addFlowLayout
{
    self.flowLayout1 = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout1.itemSize = CGSizeMake((kScreenWidth - 70) / 2, (kScreenWidth - 70) / 2 + 50 * kHeight); // item大小
    
    self.flowLayout1.scrollDirection = UICollectionViewScrollDirectionVertical; // 滑动方向
    self.flowLayout1.minimumInteritemSpacing = 30; // 列边距
    self.flowLayout1.minimumLineSpacing = 50;  // 行边距
   
    self.flowLayout1.sectionInset = UIEdgeInsetsMake(20 * kWidth, 20 * kHeight, 20 * kWidth, 20 * kHeight);  // 内边距
    // 表头的高度
    self.flowLayout1.headerReferenceSize = CGSizeMake(0, 230 * kHeight);
    
//    self.flowLayout2 = [[UICollectionViewFlowLayout alloc] init];
//    self.flowLayout2.itemSize = CGSizeMake(kScreenWidth, 300);
//    self.flowLayout2.scrollDirection = UICollectionViewScrollDirectionVertical;
//    self.flowLayout2.minimumInteritemSpacing = 0;
//    self.flowLayout2.minimumLineSpacing = 50;
    
    // 初始化 集合视图
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - self.tabBarController.tabBar.height - 20 * kHeight) collectionViewLayout:self.flowLayout1];
    // 设置代理
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor]; // 颜色
    [self.view addSubview:self.collectionView];
    
    // 注册自定义cell
    [self.collectionView registerClass:[TotalCollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
  //  [self.collectionView registerClass:[TotalSubCollectionViewCell class] forCellWithReuseIdentifier:@"MySubCell"];
    
    // 注册表头
    [self.collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader"];
    
}

#pragma mark -- CollectionView代理方法
// 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// 分区的Item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
}

// 自定义cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    TotalCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    MenuModel *model = [[MenuModel alloc] init];
  //  model = [DefaultManager defaultManager].AllArrayUrl[indexPath.row];
 //  NSArray *array = [[DefaultManager defaultManager] AllArrayUrl];
    if (self.AllArrayUrl1.count > 0) {
    
    
    if (self.AllArrayUrl1.count > 0) {
        
    
    
    if (self.index == 0) {
        
            
       
        model = self.AllArrayUrl1[indexPath.row];
        cell.upLabel.text = model.title;
        cell.downLabel.text = model.effect;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    } else if (self.index == 1) {
        model = self.AllArrayUrl1[12 * 1 + indexPath.row];
        cell.upLabel.text = model.title;
        cell.downLabel.text = model.effect;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    } else if (self.index == 2) {
        model = self.AllArrayUrl1[12 * 2 + indexPath.row];
        cell.upLabel.text = model.title;
        cell.downLabel.text = model.effect;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    } else if (self.index == 3) {
        model = self.AllArrayUrl1[12 * 3 + indexPath.row];
        cell.upLabel.text = model.title;
        cell.downLabel.text = model.effect;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    } else if( self.index == 4) {
        model = self.AllArrayUrl1[12 * 4 + indexPath.row];
        cell.upLabel.text = model.title;
        cell.downLabel.text = model.effect;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    }else if( self.index == 5) {
        model = self.AllArrayUrl1[12 * 5 + indexPath.row];
        cell.upLabel.text = model.title;
        cell.downLabel.text = model.effect;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    } else if( self.index == 6) {
        model = self.AllArrayUrl1[12 * 6 + indexPath.row];
        cell.upLabel.text = model.title;
        cell.downLabel.text = model.effect;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    }  else if( self.index == 7) {
        model = self.AllArrayUrl1[12 * 7 + indexPath.row];
        cell.upLabel.text = model.title;
        cell.downLabel.text = model.effect;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    }

        }

         }

     return cell;
}

// 设置表头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    self.headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader" forIndexPath:indexPath];
    
    if (self.AllArrayUrl1.count > 0) {
        
    
    if (self.index == 0) {
        MenuModel *model = [[MenuModel alloc] init];
        model = [DefaultManager defaultManager].ReMenMeiShiDataArray[indexPath.row];
        [self.headerView.bigImage sd_setImageWithURL:[NSURL URLWithString: model.thumb_2]];
    }else if (self.index == 1) {
        MenuModel *model = [[MenuModel alloc] init];
        model = [DefaultManager defaultManager].LiangTangDataArray[indexPath.row];;
        [self.headerView.bigImage sd_setImageWithURL:[NSURL URLWithString: model.thumb_2]];
    }else if (self.index == 2) {
        MenuModel *model = [[MenuModel alloc] init];
        model = [DefaultManager defaultManager].TeSeXiaoChiDataArray[indexPath.row];
        [self.headerView.bigImage sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    }else if (self.index == 3) {
        MenuModel *model = [[MenuModel alloc] init];
        model = [DefaultManager defaultManager].LiangCaiDataArray[indexPath.row];
        [self.headerView.bigImage sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    }else if (self.index == 4) {
        MenuModel *model = [[MenuModel alloc] init];
        model = [DefaultManager defaultManager].ShiShangYinPinDataArray[indexPath.row];
        [self.headerView.bigImage sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    }else if (self.index == 5) {
        MenuModel *model = [[MenuModel alloc] init];
        model = [DefaultManager defaultManager].TianPinnDataArray[indexPath.row];
        [self.headerView.bigImage sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    }else if (self.index == 6) {
        MenuModel *model = [[MenuModel alloc] init];
        model = [DefaultManager defaultManager].YaoShanShiLiaoDataArray[indexPath.row];
        [self.headerView.bigImage sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    }else if (self.index == 7) {
        MenuModel *model = [[MenuModel alloc] init];
        model = [DefaultManager defaultManager].HongPeiDataArray[indexPath.row];
        [self.headerView.bigImage sd_setImageWithURL:[NSURL URLWithString:model.thumb_2]];
    }
        
    }
    
    
    // button的点击事件
//    UIButton *leftButton = (UIButton *)[self.headerView.button viewWithTag:10];
//    [leftButton addTarget:self action:@selector(actionButtonLeft:) forControlEvents:(UIControlEventTouchUpInside)];
//    
//    UIButton *reghtButton = (UIButton *)[self.headerView.button viewWithTag:11];
//    [reghtButton addTarget:self action:@selector(actionButtonLeft:) forControlEvents:(UIControlEventTouchUpInside)];
//    [self.headerView.button addTarget:self action:@selector(actionButtonLeft:) forControlEvents:(UIControlEventTouchUpInside)];
    
    return self.headerView;
    
}

- (void)actionImageButton:(UIButton *)button
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
}


//- (void)actionButtonLeft:(UIButton *)button
//{
//    
//    if (button.tag == 10) {
//        NSLog(@"我是左边的e, %ld", button.tag);
//    }
//    if (button.tag == 11) {
//        NSLog(@"我是右边的, %ld", button.tag);
//    }
//}
//
//- (void)actionButtonRight:(UIButton *)button
//{
//    [self.tableView reloadData];
//    
//}

// 设置collectionView点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];

    if (self.AllArrayUrl1.count > 0) {
        
  

    if (self.AllArrayUrl1.count > 0) {
        
   
    

    if (self.index == 0) {
        self.model = self.AllArrayUrl1[indexPath.row];
        detailVC.model = self.model;
    } else if (self.index == 1) {
        self.model = self.AllArrayUrl1[12 * 1 + indexPath.row];
        detailVC.model = self.model;
    } else if (self.index == 2) {
        self.model = self.AllArrayUrl1[12 * 2 + indexPath.row];
        detailVC.model = self.model;
    } else if (self.index == 3) {
        self.model = self.AllArrayUrl1[12 * 3 + indexPath.row];
        detailVC.model = self.model;
    } else if( self.index == 4) {
        self.model = self.AllArrayUrl1[12 * 4 + indexPath.row];
        detailVC.model = self.model;
    }else if( self.index == 5) {
        self.model = self.AllArrayUrl1[12 * 5 + indexPath.row];
        detailVC.model = self.model;
    } else if( self.index == 6) {
        self.model = self.AllArrayUrl1[12 * 6 + indexPath.row];
        detailVC.model = self.model;
    }  else if( self.index == 7) {
        self.model = self.AllArrayUrl1[12 * 7 + indexPath.row];
        detailVC.model = self.model;
    }
        
         }

          }
}



@end
